﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using ARS.Control.Custom.Entity.Attributes;
using ARS.Control.Custom.Entity.Widget;

namespace ARS.Control.Custom.Entity.Common
{
    public class WhereCommon
    {

        private EntityControl _EntityControl;

        public WhereCommon(EntityControl entity)
        {
            this._EntityControl = entity;
        }

        /// <summary>
        /// 条件
        /// </summary>
        public List<WhereField> WhereFieldList
        {
            get;
            set;
        }

        private string _queryWhere;
        public string QueryWhere
        {
            get
            {
                if (_queryWhere == null)
                {
                    _queryWhere = string.Join(" and ", WhereFieldList.Select(item => { return string.Format("{0}='{1}'", item.FieldName, item.Value); }).ToArray());
                }
                return _queryWhere;
            }
            set
            {
                this._queryWhere = value;
            }
        }

        private DataSet dateSet;
        public DataSet GetDataSet()
        {
            if (this.dateSet == null)
            {
                dateSet = _EntityControl.DbProvider.Query(_EntityControl.EntityToSql.GetSelectSql(QueryWhere));
                int count = dateSet.Tables[0].Rows.Count;
                if (count == 1)
                {
                    _EntityControl.IsAdd = false;
                }
                else if (count > 0)
                {
                    HttpContext.Current.Response.Write("查询结果有" + count.ToString() + "项");
                    HttpContext.Current.Response.End();
                }
                else
                {
                    _EntityControl.IsAdd = true;
                }
            }
            return dateSet;
        }
    }
}
